package com.kxmall.huishou.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kxmall.categorize.domain.vo.HsWasteCategorizeVo;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kxmall.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.kxmall.huishou.domain.bo.HsWasteSiteBo;
import com.kxmall.huishou.domain.vo.HsWasteSiteVo;
import com.kxmall.huishou.domain.HsWasteSite;
import com.kxmall.huishou.mapper.HsWasteSiteMapper;
import com.kxmall.huishou.service.IHsWasteSiteService;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 废品回收站信息Service业务层处理
 *
 * @author kxmall
 * @date 2023-11-23
 */
@RequiredArgsConstructor
@Service
public class HsWasteSiteServiceImpl implements IHsWasteSiteService {

    private final HsWasteSiteMapper baseMapper;

    /**
     * 查询废品回收站信息
     */
    @Override
    public HsWasteSiteVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询废品回收站信息列表
     */
    @Override
    public TableDataInfo<HsWasteSiteVo> queryPageList(HsWasteSiteBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HsWasteSite> lqw = buildQueryWrapper(bo);
        Page<HsWasteSiteVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询废品回收站信息列表
     */
    @Override
    public List<HsWasteSiteVo> queryList(HsWasteSiteBo bo) {
        LambdaQueryWrapper<HsWasteSite> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HsWasteSite> buildQueryWrapper(HsWasteSiteBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HsWasteSite> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), HsWasteSite::getName, bo.getName());
        lqw.eq( HsWasteSite::getDelFlag, 0l);
        lqw.eq(StringUtils.isNotBlank(bo.getProvince()), HsWasteSite::getProvince, bo.getProvince());
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), HsWasteSite::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getCounty()), HsWasteSite::getCounty, bo.getCounty());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), HsWasteSite::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getContactor()), HsWasteSite::getContactor, bo.getContactor());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), HsWasteSite::getPhone, bo.getPhone());
//        lqw.eq(bo.getLongitude() != null, HsWasteSite::getLongitude, bo.getLongitude());
//        lqw.eq(bo.getLatitude() != null, HsWasteSite::getLatitude, bo.getLatitude());
        return lqw;
    }

    /**
     * 新增废品回收站信息
     */
    @Override
    public Boolean insertByBo(HsWasteSiteBo bo) {
        HsWasteSite add = BeanUtil.toBean(bo, HsWasteSite.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改废品回收站信息
     */
    @Override
    public Boolean updateByBo(HsWasteSiteBo bo) {
        HsWasteSite update = BeanUtil.toBean(bo, HsWasteSite.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HsWasteSite entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除废品回收站信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
//        HsWasteSite hsWasteSite = new HsWasteSite();
//        hsWasteSite.setDelFlag(1l);
//        int num = baseMapper.update(hsWasteSite,  new QueryWrapper<HsWasteSite>().in("id",ids));
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<HsWasteSiteVo> queryPageListByLatAndLon(HsWasteSiteBo bo, PageQuery pageQuery) {

        return baseMapper.queryPageListByLatAndLon(bo.getLongitude(),bo.getLatitude(), (pageQuery.getPageNum()-1)*pageQuery.getPageSize(), pageQuery.getPageSize());
    }

    @Override
    public HsWasteSiteVo queryByIdAndlatAndLon(BigDecimal longitude, BigDecimal latitude, Long id){
        return baseMapper.queryByIdAndlatAndLon(longitude, latitude, id);
    }


}
